/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import GUI.MainFunction;
import connection.ManLabConnection;
import entities.PatientDetails;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Dell
 */
public class PatientManage {

    private ArrayList<PatientDetails> patientList = new ArrayList<PatientDetails>();
    private SimpleDateFormatter sdf = new SimpleDateFormatter();
    private ManLabConnection cnn;
    private ResultSet rs,rs1;
    private String qry, qry1, qry2, qry3, qry4;
    private MainFunction mf;
    private ValidateInput vi=new ValidateInput();

    public PatientManage(ArrayList<PatientDetails> patientList, MainFunction mf) {
        this.patientList = new ArrayList<PatientDetails>();
        this.patientList = patientList;
        cnn = new ManLabConnection();
        this.mf = mf;
    }

    public PatientManage() {
    }

    public boolean createNewPatient(PatientDetails pd) {
        boolean cn=cnn.createConnection();
      
        int a;
        if (pd.ispGender()) {
            a = 1;

        } else {
            a = 0;
        }



        qry1 = "insert into PatientDetail values('" + vi.convertSpecialCharacters(pd.getpFName()) + "','" + vi.convertSpecialCharacters(pd.getpLName()) + "','" + vi.convertSpecialCharacters(pd.getpAddress()) + "','" + vi.convertSpecialCharacters(pd.getpPhone()) + "'," + a + ",'" + sdf.convertDateToString(pd.getpDOB()) + "')";

        boolean pass1 = cnn.executeQuery(qry1);


        qry = "select top 1 * from PatientDetail order by pID desc";
        rs = cnn.createResultSet(qry);
        int pid = 0;
        boolean exc2 = false;
        try {
            while (rs.next()) {
                pid = rs.getInt("pID");
                pd.setpID(pid);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManage.class.getName()).log(Level.SEVERE, null, ex);
        }

     

        cnn.closeConnection();
        if (pass1 == true) {
            this.patientList.add(pd);
       
            mf.reviewModel();
            JOptionPane.showMessageDialog(mf, "Register Succesfully!!");
            return true;
        } else {
            JOptionPane.showMessageDialog(mf, "ERROR!!!!");
            return false;
        }

    }

    public boolean updatePatient(PatientDetails pd) {
        boolean cn=cnn.createConnection();
        int a;
        if (pd.ispGender()) {
            a = 1;

        } else {
            a = 0;
        }
        qry = "update PatientDetail set pFName='" + vi.convertSpecialCharacters(pd.getpFName()) + "',pLName='" + vi.convertSpecialCharacters(pd.getpLName()) + "',pAddress=' " + vi.convertSpecialCharacters(pd.getpAddress()) + "',pPhone='" + vi.convertSpecialCharacters(pd.getpPhone()) + "',pGender=" + a + ",pDOB='" + sdf.convertDateToString(pd.getpDOB()) + "' where pID=" + pd.getpID();

        boolean pass1 = cnn.executeQuery(qry);

        cnn.closeConnection();
        if (pass1 == true) {

            mf.reviewModel();
            JOptionPane.showMessageDialog(mf, "Update Succesfully!!");
            return true;
        } else {
            JOptionPane.showMessageDialog(mf, "ERROR!!!!");
            return false;
        }


    }

    public boolean deletePatient(PatientDetails pd) {
        boolean cn=cnn.createConnection();

        qry = "select bID from Billing where pID=" + pd.getpID();
        rs = cnn.createResultSet(qry);
        int bid = 0;
        boolean exc2 = false;
        try {
            while (rs.next()) {
                bid = rs.getInt("bID");
          

            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManage.class.getName()).log(Level.SEVERE, null, ex);
        }

        qry = "select reportID from Report where pID=" + pd.getpID();
        rs1 = cnn.createResultSet(qry);
        int rid = 0;

        try {
            while (rs1.next()) {
                rid = rs1.getInt("reportID");


            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        qry="delete from Report where reportID="+rid;
        boolean pass3=cnn.executeQuery(qry);

        qry = "delete from BillDetail where bID=" + bid;
        boolean pass2 = cnn.executeQuery(qry);

        qry = "delete from Billing where bID=" + bid;
        boolean pass = cnn.executeQuery(qry);

        qry = "delete from PatientDetail where pID=" + pd.getpID();
        boolean pass1 = cnn.executeQuery(qry);
        cnn.closeConnection();
        if (pass3==true&&pass2 == true && pass1 == true && pass == true) {

    
            JOptionPane.showMessageDialog(mf, "Delete Succesfully!!");
            return true;
        } else {
            JOptionPane.showMessageDialog(mf, "ERROR!!!!");
            return false;
        }
    }
}
